package com.zhanghuang.springbootstudy01.controller;

import com.zhanghuang.springbootstudy01.bo.Employee;
import com.zhanghuang.springbootstudy01.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class HelloController {
    @Autowired
    private com.zhanghuang.template.HelloFormatTemplate template;
    @Autowired
    private com.zhanghuang.config.HelloProperties helloProperties;

    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("/sayHello")
    public String sayHello(){
        // 如果有阿里的包（com.alibaba.fastjson）用 json format
        //        // 没有，用string formatz
        return template.doFormat(helloProperties);
    }

    public List<Employee> getEmployees(){
        return employeeMapper.getEmployees();
    }


    @GetMapping("/save")
    public int save() {
        Employee employee = new Employee();
        employee.setLastName("zhanghuang");
        employee.setEmail("1098799@qq.com");
        employee.setGender(1);
        employee.setDepartment(101);
        employee.setBirth(new Date());
        return employeeMapper.save(employee);
    }
    @GetMapping("/get/{id}")
    public Employee get(@PathVariable("id") Integer pid){
     return  employeeMapper.get(pid);
    }

    @GetMapping("/delete/{id}")
    public int delete(@PathVariable("id") Integer id) {
        return employeeMapper.delete(id);
    }
}
